package com.example.mystoryapp2.ui.main

import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import com.example.mystoryapp2.R
import com.example.mystoryapp2.ui.base.BaseActivity
import com.example.mystoryapp2.databinding.ActivityMainLayoutBinding


class MainActivity : BaseActivity<ActivityMainLayoutBinding, MainViewModel>() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initView()
    }

    override fun initView() {
        db.navView.selectedItemId = 0
        onItemChange(R.id.home)
        db.navView.setOnItemSelectedListener { item ->
            onItemChange(item.itemId)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_main_layout
    }

    override fun getViewModel(): MainViewModel {
        return ViewModelProvider(
            this, ViewModelProvider
                .AndroidViewModelFactory(application)
        )[MainViewModel::class.java]
    }

    override fun setViewModel() {
        db.viewModel = getViewModel()
    }

    private fun onItemChange(item: Int): Boolean {
        val beginTransaction = supportFragmentManager.beginTransaction()
        db.viewModel?.getFragment(item)?.let {
                beginTransaction.replace(
                    R.id.rv,
                    it
                ).commit()
        }
        return true
    }

}